package doyouknow.group.CnAndZhb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import doyouknow.group.CnAndZhb.entity.TbAdmin;
import doyouknow.group.CnAndZhb.dao.master.TbAdminDao;
import doyouknow.group.CnAndZhb.error.BusinessException;
import doyouknow.group.CnAndZhb.error.EnumBusinessError;
import doyouknow.group.CnAndZhb.service.TbAdminService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TbAdmin)表服务实现类
 *
 * @author cn
 * @since 2020-11-03 20:50:41
 */
@Service("tbAdminService")
public class TbAdminServiceImpl implements TbAdminService {
    @Resource
    private TbAdminDao tbAdminDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TbAdmin queryById(Integer id) {
        return this.tbAdminDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<TbAdmin> queryAllByLimit(int offset, int limit) {
        return this.tbAdminDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param tbAdmin 实例对象
     * @return 实例对象
     */
    @Override
    public TbAdmin insert(TbAdmin tbAdmin) {
        this.tbAdminDao.insert(tbAdmin);
        return tbAdmin;
    }

    /**
     * 修改数据
     *
     * @param tbAdmin 实例对象
     * @return 实例对象
     */
    @Override
    public TbAdmin update(TbAdmin tbAdmin) {
        this.tbAdminDao.update(tbAdmin);
        return this.queryById(tbAdmin.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tbAdminDao.deleteById(id) > 0;
    }

    @Override
    public TbAdmin validateAdminLogin(String username, String password,int type) throws BusinessException {

        QueryWrapper<TbAdmin> queryWrapper = new QueryWrapper<>();


        String CheckValue = "username";

        if(type == 2)
            CheckValue = "phoneNumber";

//        queryWrapper.eq(CheckValue,username).eq("type",type);
//        queryWrapper.eq(CheckValue,username);
        TbAdmin findObj = tbAdminDao.queryByUserName(username);

//
        if(findObj == null){
            throw new BusinessException(EnumBusinessError.USER_LOG_IN_FAIL);
        }

        if(!findObj.getPassword().equals(password)){
            throw new BusinessException(EnumBusinessError.USER_LOG_IN_FAIL);
        }

        return findObj;
    }
}